package com.lpf.example.example02.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;

public class MyClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

        Thread.sleep(2000);

        System.out.println(ctx.channel().remoteAddress());

        // 服务器端回应的数据
        System.out.println("client output:" + msg);

        // 客户端发给服务器端的数据
        ctx.writeAndFlush("from client:" + LocalDateTime.now());
    }

    // 在这里触发最开始的消息发送
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush("来自客户端的问候");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}